package com.sea.finance.service.impl;

import com.codecrane.core.power.entity.DictDetail;
import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.Fund;

import com.sea.finance.entity.FundType;
import com.sea.finance.mapper.FundMapper;
import com.sea.finance.service.FundService;
import com.sea.finance.service.FundTypeService;
import com.sea.operate.entity.Related;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class FundServiceImpl implements FundService {
    @Autowired
    private FundMapper fundMapper;
    @Autowired
    private DictDetailService dictDetailService;
    @Autowired
    private FundTypeService fundTypeService;

    @Override
    public List<Fund> findByCnd(Fund fund) {
        return fundMapper.findByCnd(fund);
    }

    @Override
    public int save(Fund fund) {
        return fundMapper.insert(fund);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importFund(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<Fund> list = BatchImportUtils.iteratorForFund(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条信息！");
            } else {
                for(Fund fund:list){
                    List<Fund> byCnd = findByCnd(fund);
                    if(byCnd.size()==0){
                        save(fund);
                    }
                }
                List<Fund> shops = findByShop();
                for(Fund fund:shops) {
                    FundType fundType = new FundType();
                    fundType.setFundMoneyType(fund.getFundMoneyType());
                    fundType.setFundShopName(fund.getFundShopName());
                    FundType t = fundTypeService.findByShop(fundType);
                    if (null == t) {
                        fundTypeService.save(fundType);
                    }
                    fundType.setFundTotal(fund.getFundTotal());
                    fundTypeService.updateTotal(fundType);
                }
            }
        }
    }

    @Override
    public List<Fund> findByShop() {
        return fundMapper.findByShop();
    }

    @Override
    public Fund findTotalByShop(Fund fund) {
        return fundMapper.findTotalByShop(fund);
    }

    @Override
    public Fund findByOrderId(String fundOrderId) {
        return fundMapper.findByOrderId(fundOrderId);
    }
}
